package com.blackhorse.annotation.dao;

import com.blackhorse.domain.Account;
import com.blackhorse.domain.AccountUser;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface AAccountMapper {
    @Select("select * from account")
    public List<Account> findAll();

    @Select("select * from account where id = #{value}")
    public Account findById(int id);

    //根据用户id查询账户
    @Select("select * from account where uid = #{value}")
    public List<Account> findByUid(int uid);

    //查询所有账户，并关联查询账户的用户信息
    @Select("select * from account")
    @Results(id = "", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "money", property = "money"),
            @Result(property = "user", column = "uid",
                    one = @One(select = "com.blackhorse.annotation.dao.AUserMapper.findById", fetchType = FetchType.EAGER))
    })
    public List<Account> findAllAccountWithUser();

    //查询所有账户，并关联查询账户的用户信息（使用延迟加载）
    @Select("select * from account")
    @Results(id = "", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "money", property = "money"),
            @Result(property = "user", column = "uid",
                    one = @One(select = "com.blackhorse.annotation.dao.AUserMapper.findById", fetchType = FetchType.LAZY))
    })
    public List<Account> findAllAccountWithUserByLazyLoading();
}
